$(function () {
  getArr()
})
function getArr() {
  // 渲染
  axios.get('/my/userinfo')
  .then(res => {
      let data = res.data.data
      let welcome = document.querySelector('#welcome')
      let layui_nav_img = document.querySelectorAll('.layui-nav-img')
      let text_avatar = document.querySelectorAll('.text-avatar')
      welcome.innerHTML = `欢迎${data.nickname !== '' ? data.nickname : data.username}`
      if (data.user_pic !== null) {
        layui_nav_img.forEach(item => {
          item.src = data.user_pic
        })
        text_avatar.forEach(item => {
          item.style.display = 'none'
        })
      } else {
        layui_nav_img.forEach(item => {
          item.style.display = 'none'
        })
        text_avatar.forEach(item => {
          item.innerHTML = Array.from(data.username)[0]
        })
      }
    })
  // 退出功能
  let btnLogout = document.querySelector('#btnLogout')
  btnLogout.addEventListener('click', function () {
    layer.confirm('确定退出登录?', { icon: 3, title: '提示' }, function (index) {
      // 清空localStorage并且退回登录页面      
      localStorage.removeItem('token')
      location.href = '/login.html'
      layer.close(index);
    });
  })
}